package dtaskboard.client.charts;

import com.google.gwt.visualization.client.AbstractDataTable.ColumnType;
import com.google.gwt.visualization.client.DataTable;
import com.google.gwt.visualization.client.visualizations.corechart.LineChart;
import com.google.gwt.visualization.client.visualizations.corechart.Options;

import dtaskboard.client.dto.ProjectDTO;
import dtaskboard.client.dto.SprintDTO;

/**
 * Classe que cria o burndownChart a partir de um sprint.
 * 
 * @author meiao
 * 
 */
public class ProjectBurnupChart {

  public static LineChart build(ProjectDTO project) {
    Options options = Options.create();
    options.setWidth(400);
    options.setHeight(240);
    options.setTitle("Project Burnup");

    int sprints = (int) (project.getEstimatedSprints() + 1);
    DataTable data = DataTable.create();
    data.addColumn(ColumnType.NUMBER, "Ideal");
    data.addColumn(ColumnType.NUMBER, "Real");

    data.addRows(sprints);
    long total = project.getHours();
    for (int i = 0; i < sprints; i++) {
      data.setValue(i, 0, i * total / sprints);
    }

    data.setValue(0, 1, 0);

    int i = 0;
    long sum = 0;
    for (SprintDTO sprint : project.getSprints()) {
      if (sprint.getAchievedHours() > 0) {
        i++;
        sum += sprint.getAchievedHours();
        data.setValue(i, 1, sum);
      } else {
        break;
      }
    }

    return new LineChart(data, options);
  }

}
